package com.sk.weichat.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.util.AppUtils;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.ClearEditText;
import com.sk.weichat.view.MaxTextTwoLengthFilter;

/**
 * 个性签名
 * create by swy 2019-06-25
 */
public class SetSignActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEditSign;
    private TextView mTvEditNum;
    private ImageView image_x;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_sign);
        initActionBar();
        initView();

    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.mergerStatus).setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        TextView tvTitle = (TextView) findViewById(R.id.tv_title_center);
        tvTitle.setText(R.string.diy_name);
        TextView tvRight = (TextView) findViewById(R.id.tv_title_right);
        image_x = findViewById(R.id.image_x);
        tvRight.setText(R.string.finish);
        tvRight.setTextColor(ContextCompat.getColor(this, R.color.deep_purple));
        tvRight.setOnClickListener(this);
        findViewById(R.id.iv_title_left).setOnClickListener(this);
        image_x.setOnClickListener(this);
    }

    private void initView() {
        mEditSign = findViewById(R.id.edit_sign);
        mTvEditNum = findViewById(R.id.tv_edit_num);
        mEditSign.addTextChangedListener(new MyTextWatcher());
        mEditSign.setText(getIntent().getStringExtra("sign"));
        try {
            mEditSign.setSelection(getIntent().getStringExtra("sign").length());
        } catch (Exception e) {

        }
        mEditSign.setFilters(new InputFilter[]{new MaxTextTwoLengthFilter(60)});

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;

            case R.id.tv_title_right:
                Intent intent = new Intent();
                intent.putExtra("sign", mEditSign.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.image_x:
                mEditSign.setText("");
                break;
        }
    }


    private class MyTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            if (s.length()>0){
//            }else {
//            }
//            if(s.length()>30){
//                ToastUtil.showToast(getApplicationContext(),"个性签名长度不能超过30位");
//                mEditSign.setText(s.subSequence(0,30));
//                mEditSign.setSelection(30);
//                mTvEditNum.setText("30/30");
//            }else{
//                mTvEditNum.setText(s.length()+"/30");
//            }

            if (s.length() == 0) {
                image_x.setVisibility(View.GONE);
                mTvEditNum.setText("0/60");
            } else {
                image_x.setVisibility(View.VISIBLE);
                mTvEditNum.setText(AppUtils.judgeTextLength(s.toString()) + "/60");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
